package com.luoran.car.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.luoran.car.config.UploadMinioUtils2;
import com.luoran.car.domain.SysDriver;
import com.luoran.car.service.SysDriverService;
import com.luoran.car.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("driver")
public class DriverController {

    @Autowired
    SysDriverService sysDriverService;

    @RequestMapping("driverList")
    public Result driverList(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize, @RequestBody SysDriver sysDriver) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysDriver> list = sysDriverService.driverList(sysDriver);
        PageInfo<SysDriver> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }

    @RequestMapping("driverAdd")
    public Result driverAdd(@RequestBody SysDriver sysDriver) {
        sysDriver.setCreateTime(new Date());
        boolean save = sysDriverService.save(sysDriver);
        return save ? Result.success() : Result.fail();
    }

    @RequestMapping("driverUpdate")
    public Result driverUpdate(@RequestBody SysDriver sysDriver) {
        boolean b = sysDriverService.updateById(sysDriver);
        return b ? Result.success() : Result.fail();
    }

    @RequestMapping("driverDelete")
    public Result driverDelete(Integer driverId) {
        boolean removeById = sysDriverService.removeById(driverId);
        return removeById ? Result.success() : Result.fail();
    }

    @RequestMapping("driverUpload")
    public String driverUpload(MultipartFile file) {
        String pic = UploadMinioUtils2.uploadPic(file);
        return pic;
    }
}
